package com.fmatos.amazingwallpapers.service.image;

import java.util.Random;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import com.fmatos.amazingwallpapers.image.database.ImagesDataSource;
import com.fmatos.amazingwallpapers.service.alarm.AlbumAlarm;


public class BitmapsService  extends Service{
	
	private static final String TAG = BitmapsService.class.getSimpleName();
	private final IBinder mBinder = new LocalBinder();

	
	
	private BitmapsWorker thDownloader;

	private ImagesDataSource dataSource;
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {

		Toast.makeText(this, "Hello from MyService!!", Toast.LENGTH_LONG).show();
		Log.d(TAG, "Hello from MyService!");
		
		if ( intent != null ) {
			
//			onCreate();
			
			if (AlbumAlarm.BROADCAST_ACTION_DOWNLOAD_INFO.equals(intent.getAction())) {
				getAlbumsInfo();
				
			} else 	if (AlbumAlarm.BROADCAST_ACTION_DOWNLOAD_IMAGES.equals(intent.getAction())) {
				getNewestImages();
//			} else if ( ACTION_DOWNLOAD_SINGLE_URL.equals(intent.getAction())) {
//				String url = intent.getExtras().getString(EXTRA_URL);
//				downloadSingleImage(url);
//			
			}
			
		}
			
		
		Log.d(TAG, "Goodbye from MyService!");
		

		
//		onDestroy();
//		stopSelf();
		
		thDownloader.stopServiceDelayed(this); // sends a stopSelf message to end of queue, so messages get the chance of being processed

		return super.onStartCommand(intent, flags, startId);
	}
	

	public void onCreate() {
		Log.i(TAG,"Open database");
		
		dataSource = new ImagesDataSource(getApplicationContext());
		dataSource.open(); // TODO: do this in background
	
		thDownloader = new BitmapsWorker(TAG,dataSource,getApplicationContext());
		thDownloader.start();
	}
	

	public void onDestroy() {
		Log.i(TAG,"Attempting to close database");
		stopWorker();
		Log.i(TAG,"Close database");
		dataSource.close();
	}

	@SuppressLint("NewApi")
	private void stopWorker() {
		
		thDownloader.stopHandler();
		thDownloader.interrupt();
//		thDownloader.quitSafely(); // this call requires API 18
	 	// TODO must stop handler before quitting
//		thDownloader.interrupt();		
	}


	@Override
	public IBinder onBind(Intent intent) {
		
		return mBinder;
	}
	
	
    public class LocalBinder extends Binder {
    	public BitmapsService getService() {
            // Return this instance of LocalService so clients can call public methods
            return BitmapsService.this;
        }
    }
    
    private final Random mGenerator = new Random();
    
    /** method for clients */
    public int getRandomNumber() {
      return mGenerator.nextInt(100);
    }
    
    public void getAlbumsInfo() {
		thDownloader.getAlbumsInfo();
    }

    public void getNewestImages() {
		thDownloader.getNewestImages();
    }
    
	public void addCommentInBackground(String comment) {
//		createComment(comment);
		
	}
	
	public void testFromAlarm() {
		Log.i(TAG,"Call service from alarm");
	}

}

